#!/bin/sh
set -e  # die as fast as possible

echo "This is an a script to make a virtualenv working under Python3.9";
echo "   "

SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  REPO_ROOT_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
REPO_ROOT_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"

# we could configure and upgrade to newer python in the future
PYTHON=python3.9
DEFAULT_VENV_NAME="venv"
ALT_GDAL_PATH=$(python $REPO_ROOT_DIR/infra/binary_dependencies/versions.py GDAL_INSTALLED_DIR)
DIR_PYTHON_PACKAGES="infra/requirements"

if ! command -v ${PYTHON} &> /dev/null
then
    echo "${PYTHON} could not be found, please contact the system admin to install it first"
    exit
fi

if ! command -v virtualenv &> /dev/null
then
    echo "virtualenv could not be found, please install it first"
    exit
fi

if ! command -v gdal-config &> /dev/null
then
    echo "gdal could not be found, please contact the system admin to install it first"
    exit
fi
GDAL_VERSION=`gdal-config --version`

if ! command -v `${ALT_GDAL_PATH}/bin/gdal-config` &> /dev/null
then
    echo "did not find provisioned higher version gdal, skip."
    ALT_GDAL_VERSION=""
else
    ALT_GDAL_VERSION=`${ALT_GDAL_PATH}/bin/gdal-config --version`
    echo "found GDAL ${GDAL_VERSION} and ${ALT_GDAL_VERSION}"
fi

if [ -z "$1" ]
then
    VENV_DIR=$REPO_ROOT_DIR
    VENV_DIR+="/$DEFAULT_VENV_NAME"
    echo "creating a virtualenv at the default place: $VENV_DIR"
else
    VENV_DIR=$1
    echo "creating a virtualenv at the designated place: $VENV_DIR"
fi

read -p "Are you sure? Type Y or y to proceed: " -n 1 -r
echo    # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
    if [ -z "${ALT_GDAL_VERSION}" ]
    then
        GDAL_TOINSTALL="DEFAULT"
    else
        read -p "Which gdal to install? Type L for legacy gdal ${GDAL_VERSION} or any other input for ${ALT_GDAL_VERSION}: " -n 1 -r
        echo    # (optional) move to a new line
        if [[ $REPLY =~ ^[L]$ ]]
        then
            GDAL_TOINSTALL="DEFAULT"
        else
            GDAL_TOINSTALL="ALT"
        fi
    fi
    read -p "Do you want to install version for Parcel Segmentation (MaskDINO) [Not common]? Type P for 'Yes' or any other input for 'NO': " -n 1 -r
    if [[ $REPLY =~ ^[P]$ ]]
    then
        echo    # (optional) move to a new line
        echo  "You will install the environment specifically for Parcel Segmentation (MaskDINO)"
        PARCEL_TOINSTALL="YES"
    else
        PARCEL_TOINSTALL="NO"
    fi
else
    echo "bye"
    exit
fi

$PYTHON -m venv $VENV_DIR
echo "installing requirements other than gdal"
$VENV_DIR/bin/pip install -r $REPO_ROOT_DIR/$DIR_PYTHON_PACKAGES/requirements_py39.txt

echo "installing several additional requirements"
$VENV_DIR/bin/pip install -r $REPO_ROOT_DIR/$DIR_PYTHON_PACKAGES/requirements_additional.txt

echo "installing requirements for linting"
$VENV_DIR/bin/pip install -r $REPO_ROOT_DIR/$DIR_PYTHON_PACKAGES/requirements_linting.txt

echo "installing updates to typed-ast for fixing errors from linting"
$VENV_DIR/bin/pip install typed-ast --upgrade

echo "installing requirements for interactive development"
$VENV_DIR/bin/pip install -r $REPO_ROOT_DIR/$DIR_PYTHON_PACKAGES/requirements_dev.txt

if [ ${GDAL_TOINSTALL} == "ALT" ]
then
    echo "installing pygdal ${ALT_GDAL_VERSION}"
    LD_LIBRARY_PATH=${ALT_GDAL_PATH}/lib/:$LD_LIBRARY_PATH $VENV_DIR/bin/pip install --global-option=build_ext --global-option=-I${ALT_GDAL_PATH}/include --global-option=-L${ALT_GDAL_PATH}/lib GDAL==${ALT_GDAL_VERSION}
    $VENV_DIR/bin/python $REPO_ROOT_DIR/infra/binary_dependencies/patch_activate_shell_script.py $VENV_DIR
else
    echo "installing pygdal ${GDAL_VERSION}"
    $VENV_DIR/bin/pip install --global-option=build_ext --global-option="-I/usr/include/gdal" GDAL==${GDAL_VERSION}
fi

if [ ${PARCEL_TOINSTALL} == "YES" ]
then
    echo "installing requirements for supervised parcel segmentation (MaskDINO)"
    $VENV_DIR/bin/pip install -r $REPO_ROOT_DIR/$DIR_PYTHON_PACKAGES/requirements_parcel.txt

    echo "installing requirements for pixel decoder (MaskDINO)"
    source $VENV_DIR/bin/activate
    cd $REPO_ROOT_DIR/analytics/parcel_segmentation_sup/maskdino/modeling/pixel_decoder/ops
    sh make.sh

    echo "installing skywalker in dev mode"
    cd $REPO_ROOT_DIR
    $VENV_DIR/bin/pip install -e $REPO_ROOT_DIR
    deactivate
    echo "finish creating the ${DEFAULT_VENV_NAME} under Python3.9 for supervised parcel segmentation (MaskDINO)"
else
    echo "installing skywalker in dev mode"
    $VENV_DIR/bin/pip install -e $REPO_ROOT_DIR
    echo "finish creating the ${DEFAULT_VENV_NAME} under Python3.9"
fi
